package com.youlu.campus.admin.ca.controller;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.CaGroupInfo;
import com.youlu.campus.entity.CaInfo;
import com.youlu.campus.entity.VO.req.CaGroupInfoQueryVO;
import com.youlu.campus.entity.VO.req.CaInfoQueryVO;
import com.youlu.campus.entity.VO.req.CopyCa;
import com.youlu.campus.service.ca.CaGroupInfoService;
import com.youlu.campus.service.ca.CaInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

/**
 * 证书
 */
@Slf4j
@RestController
public class CaController {
    @Autowired
    private CaInfoService caInfoService;
    @Autowired
    private CaGroupInfoService caGroupInfoService;

    @PostMapping("/ca/list")
    public RestResult list(@RequestBody @Valid CaInfoQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<CaInfo> users = caInfoService.list(req);
        if (users == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            users = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(users).build();
    }

    @PostMapping("/ca/list/activity")
    public RestResult listByActivityId(@RequestBody @Valid CaInfoQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<CaInfo> users = caInfoService.listByGroupId(req);
        if (users == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            users = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(users).build();
    }

    @PostMapping("/ca/create")
    public RestResult create(@RequestBody @Valid CaInfo req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        boolean re = caInfoService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/ca/update")
    public RestResult update(@RequestBody @Valid CaInfo req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        boolean re = caInfoService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/ca/delete")
    public RestResult delete(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = caInfoService.delete(comId, id);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/ca/find-one")
    public RestResult findOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        CaInfo caInfo = caInfoService.findOne(id);
        if (Objects.nonNull(caInfo)) {
            return RestResultBuilder.builder().success(caInfo).build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    /**
     * 导入证书信息
     *
     * @param
     * @return
     */
    @PostMapping("/ca/import")
    public RestResult importCa(@RequestParam("file") MultipartFile file,
                               @RequestParam("caGroupId") String caGroupId) {
        try {
            String fileName = file.getOriginalFilename();
            log.info(":>>> 上传文件为:{}", fileName);
            Map<String, Object> res = caInfoService.caImport(file.getBytes(), fileName, caGroupId);
            return RestResultBuilder.builder().success(res).build();
        } catch (Exception e) {
            log.info(":>>> 导入证书信息错误:{}", e);
            return RestResultBuilder.builder().failure().build();
        }
    }

    @PostMapping("/ca/group/list")
    public RestResult caGroupList(@RequestBody @Valid CaGroupInfoQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<CaGroupInfo> users = caGroupInfoService.list(req);
        if (users == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            users = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(users).build();
    }

    @PostMapping("/ca/group/create")
    public RestResult createGroup(@RequestBody @Valid CaGroupInfo req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        boolean re = caGroupInfoService.create(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/ca/group/update")
    public RestResult updateGroup(@RequestBody @Valid CaGroupInfo req) {
        String comId = AuthUtils.getComId();
        req.setTenantId(comId);
        boolean re = caGroupInfoService.update(req);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/ca/group/delete")
    public RestResult deleteGroup(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        boolean re = caGroupInfoService.delete(comId, id);
        if (re) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/ca/group/find-one")
    public RestResult findGroupOne(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        CaGroupInfo caInfo = caGroupInfoService.findOne(id);
        if (Objects.nonNull(caInfo)) {
            return RestResultBuilder.builder().success(caInfo).build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    /**
     * 复制证书序列
     * @param req
     * @param bindingResult
     * @return
     */
    @PostMapping("/ca/copy")
    public RestResult copyCaInfo(@RequestBody @Valid CopyCa req,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
           return RestResultBuilder.builder().failure(bindingResult.getFieldError().getDefaultMessage()).build();
        }
        caGroupInfoService.copyCa(req);
        return RestResultBuilder.builder().success().data(null).build();
    }
}
